package com.lhl.security.jwt.domain.dto;

import com.lhl.security.jwt.domain.entity.Role;
import com.lhl.security.jwt.domain.entity.SysUser;
import io.swagger.annotations.Api;
import lombok.Getter;
import lombok.Setter;
import org.hibernate.annotations.GeneratorType;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;

/**
 * @className: com.lhl.security.jwt.domain.dto.JwtUser
 * @description: TODO 类描述
 * @author: king
 * @date: 2020-11-09
 **/
@Api(tags = "JwtUser", produces = "")
@Getter
@Setter
public class JwtUser implements UserDetails {

    private Integer id;// 用户ID
    private String username;// 用户名
    private String password;// 密码
    private List<Role> roles;// 角色
    /**
     * 用户所有权限
     */
    private List<GrantedAuthority> authorities = new ArrayList<GrantedAuthority>();


    public JwtUser() {
    }

    // 写一个能直接使用user创建jwtUser的构造器
    public JwtUser(SysUser user) {
        id = user.getId();
        username = user.getUsername();
        password = user.getPassword();
        GrantedAuthority grantedAuthority = new SimpleGrantedAuthority(user.getRole());
        authorities.add(grantedAuthority);
        authorities.add(new SimpleGrantedAuthority("sys:menu:add"));
    }

    /**
     * 封装了权限信息
     * @return
     */
    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        return authorities;
    }

    public String getPassword() {
        return password;
    }

    public String getUsername() {
        return username;
    }
    @Override
    public boolean isAccountNonExpired() {
        return true;
    }
    @Override
    public boolean isAccountNonLocked() {
        return true;
    }
    @Override
    public boolean isCredentialsNonExpired() {
        return true;
    }

    public boolean isEnabled() {
        return true;
    }

    @Override
    public String toString() {
        return "JwtUser{" +
                "id=" + id +
                ", username='" + username + '\'' +
                ", password='" + password + '\'' +
                ", authorities=" + authorities +
                '}';
    }
}
